package com.futures.questionbank.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.futures.questionbank.Constants.BaseBean;
import com.futures.questionbank.Constants.Constants;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.adapter.BaseViewHolder;
import com.futures.questionbank.adapter.SectionAdpter2;
import com.futures.questionbank.base.BaseFragment;
import com.futures.questionbank.db.table.zixun;
import com.futures.questionbank.http.CallBack;
import com.futures.questionbank.http.HttpClient;
import com.futures.questionbank.main.RegisterActivity;
import com.google.android.material.tabs.TabLayout;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2020/6/15 0015 16:07
 * version: 1.0
 */
public class InformationFragment extends BaseFragment {
    private RecyclerView rv;

    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<BaseFragment> fragments = new ArrayList<>();
    private List<String> tabs = new ArrayList<>();


    @Override
    protected int getLayoutResource() {
        return R.layout.information_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        rv =  rootView.findViewById(R.id.rv);
        UItils.showDialog(getActivity());
        List<zixun > sections1 = SQLite.select().
                from(zixun.class).queryList();

        fragments.clear();
        tabs.clear();
        tabs.add("考试动态");
        tabs.add("考试大纲");
        tabs.add("辅导资料");
        fragments.add(new OrderSettlementFragment());
        fragments.add(new OrderSettlementFragment2());
        fragments.add(new OrderSettlementFragment3());

        //设置TabLayout的模式
        viewPager.setAdapter(new TabAdapter(getChildFragmentManager()));
        //关联ViewPager和TabLayout
        tabLayout.setupWithViewPager(viewPager);
      //  viewPager.setCurrentItem(position);
        //viewpaer一次加载全部页面
        viewPager.setOffscreenPageLimit(fragments.size());


        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置Item增加、移除动画
        rv.setItemAnimator(new DefaultItemAnimator());
        SectionAdpter2 chapterAdpter = new SectionAdpter2(getActivity(), sections1, new TypeItemListener());
        rv.setAdapter(chapterAdpter);

        Map<String, Object> params = new HashMap<>();
        params.put("zixuntype","0");
        params.put("username","");
        params.put("userid","0");
        params.put("type","");
        params.put("token","C122F0975DA798DBE977426076661FC3");
        params.put("tid","422");
        params.put("sid","30");
        params.put("pageSize","10");
        params.put("pageIndex","1");
        params.put("localetoken","");
        params.put("keyword","");
        params.put("iden","AE0070A980069E0B83396096D83A57B3_xxx");
        params.put("GlobalAppType","55");
        params.put("cid","2");
      /*  HttpClient.post(getActivity(), Constants.HOST, params,new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean result) {  //1234567a

            }
        });*/
    }

    class TypeItemListener implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
            Intent intent = new Intent();
           // intent.putExtra("index", sections1.get(position).getExam_id());
           // intent.setClass(SectionActivity.this, ExamActivity.class);
            startActivity(intent);
        }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }


    class TabAdapter extends FragmentPagerAdapter {
        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //显示标签上的文字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabs.get(position);
        }
    }
}
